﻿Option Strict On
'    ISM Tools
'    Copyright (C) 2010  Nathan Johnson
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.
Imports System
Imports System.Management

Public Class Form1

    ' Modular Variables
    Const SECONDS_IN_MINUTES_Integer As Integer = 60
    Const MS_IN_SECONDS_Integer As Integer = 1000

    Private Sub Calculation_Event()
        ' Calculate Button

        ' TODO: Make sure variables are properly named
        ' Local Variables
        Dim DiskSizeInteger As Integer
        Dim DiskRPMInteger As Integer
        Dim DiskAvgRotationDelayDecimal As Decimal
        Dim DiskSeekTimeInteger As Integer
        Dim DiskBlockSizeInteger As Integer
        Dim DiskTransferRateInteger As Integer
        Dim DiskRotationTimeDecimal As Decimal
        Dim DiskTransferTimeDecimal As Decimal
        Dim DiskMaxUtilizationInteger As Integer
        Dim TotalSeekTimeDecimal As Decimal
        Dim TotalIOPDiskDecimal As Decimal
        Dim TotalIOPUtilizationInteger As Integer

        ' Grab from GUI
        Try
            DiskSizeInteger = Integer.Parse(DiskSizeTextBox.Text)
        Catch ex As Exception

            ErrorHandler.SetError(DiskBlockSizeTextBox, "Error: " & ex.Message)
        End Try

        Try
            DiskRPMInteger = Integer.Parse(DiskRPMTextbox.Text) ' Disk RPMs
        Catch ex As Exception
            ErrorHandler.SetError(DiskRPMTextbox, "Error: " & ex.Message)
        End Try

        Try
            DiskAvgRotationDelayDecimal = Decimal.Parse(DiskAvgRotationTextBox.Text) ' Average Rotation Delay
        Catch ex As Exception
            ErrorHandler.SetError(DiskAvgRotationTextBox, "Error: " & ex.Message)
        End Try

        Try
            DiskSeekTimeInteger = Integer.Parse(DiskSeekTimeTextBox.Text) ' Average Seek Time
        Catch ex As Exception
            ErrorHandler.SetError(DiskSeekTimeTextBox, "Error: " & ex.Message)
        End Try

        Try
            DiskBlockSizeInteger = Integer.Parse(DiskBlockSizeTextBox.Text) ' Application Block Size
        Catch ex As Exception
            ErrorHandler.SetError(DiskBlockSizeTextBox, "Error: " & ex.Message)
        End Try

        Try
            DiskTransferRateInteger = Integer.Parse(DiskTransferRateTextBox.Text) ' Disk Transfer Rate
        Catch ex As Exception
            ErrorHandler.SetError(DiskTransferRateTextBox, "Error: " & ex.Message)
        End Try

        Try
            DiskMaxUtilizationInteger = Integer.Parse(ControllerMaxTextBox.Text)
        Catch ex As Exception
            ErrorHandler.SetError(ControllerMaxTextBox, "Error: " & ex.Message)
        End Try

        ' Calculate
        Try
            ' Disk Rotation
            DiskRotationTimeDecimal = CDec((DiskAvgRotationDelayDecimal / DiskRPMInteger * SECONDS_IN_MINUTES_Integer * MS_IN_SECONDS_Integer)) '

            ' Disk Service
            DiskTransferTimeDecimal = CDec((DiskBlockSizeInteger / DiskTransferRateInteger)) ' Block Size / Transfer Rate

            ' Seek Time
            ' Time required to perform one I/O is sum of seek time, rotational delay and transfer time
            TotalSeekTimeDecimal = (DiskSeekTimeInteger + DiskRotationTimeDecimal + DiskTransferTimeDecimal)

            ' Total IOPs
            TotalIOPDiskDecimal = CDec(MS_IN_SECONDS_Integer / TotalSeekTimeDecimal)
            ' Efficient IOP Utilization (Turn user input into a decimal)
            TotalIOPUtilizationInteger = CInt((TotalIOPDiskDecimal * (DiskMaxUtilizationInteger / 100)))

        Catch ex As ArithmeticException
            MessageBox.Show("Error: " & ex.Message, "Error with Calculation", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "General Error... You broke it!  =( ", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

        ' Send to GUI
        ResultAccessTimeLabel.Text = TotalSeekTimeDecimal.ToString("N1")

        ResultIOPLabel.Text = TotalIOPDiskDecimal.ToString("N0")

        ResultUtilizationTextBox.Text = TotalIOPUtilizationInteger.ToString("N0")

    End Sub
    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

        Call Calculation_Event()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        ' Clear Button

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        ' Exit Button
        Me.Close()
    End Sub

    ' Tool Strip
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' Exit Menu Item
        Me.Close()

    End Sub

    Private Sub DisplaySystemButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplaySystemButton.Click
        ' Computer Information Button

        ' TODO: 
        '
        Dim Query As New SelectQuery("Win32_Processor")
        '
        Dim Searcher As New ManagementObjectSearcher(Query)
        '
        Dim WinInfo As ManagementObject

        For Each WinInfo In Searcher.Get()
            Dim ListItem As ListViewItem = ListView1.Items.Add(WinInfo.Item("Name").ToString)
            ListView1.Items.Add(WinInfo.Item("NumberOfCores").ToString())
        Next

    End Sub

End Class
